#include "BMKeyPad.h"

BMKeyPad::BMKeyPad(QWidget* widgetText, QWidget *parent, Qt::WFlags flags): QWidget(parent, flags)
{
	m_verticalLayoutKeyPadMain=NULL;
	m_verticalControlsLayout=NULL;
	m_gridLayoutKeyPadNumero=NULL;
	m_HorizontalNumControl=NULL;
	m_animacion=NULL;
	m_grupoBotones=NULL;
	m_textWidget=NULL;
	m_textWidgetAUX=NULL;
	m_campoTexto.clear();

	m_isShow=false;

	m_tamBarraBotones.setHeight(0);
	m_tamBarraBotones.setWidth(0);

	if(widgetText)
		setWidgetText(widgetText);

	this->setObjectName("main");
	this->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint|Qt::Widget);
	this->setStyleSheet( "QWidget#main{ border-image: url(\"./resources/pruebabordes.png\"); border-radius: 30px; }");//background-color: QLinearGradient(x1:0,y1:0,x2:0,y2:0.5,stop: 0.5 #7db9e8, stop: 0 #1E5799, stop: 0.5 #2989D8, stop: 0.51 #207cca, stop: 1 #7db9e8, stop: 0.899 #1E5799, stop: 1 #1E5799 ); }" );
	this->setAttribute(Qt::WA_DeleteOnClose);
	
	CargarKeyPad();

	m_XKEYPAD=-this->width();
	m_YKEYPAD=0;
	this->move(m_XKEYPAD, m_YKEYPAD);
}

BMKeyPad::~BMKeyPad()
{
	m_textWidget=NULL;
	m_textWidgetAUX=NULL;
}

void BMKeyPad::paintEvent(QPaintEvent* PE)
{
	QStyleOption o;
	o.initFrom(this);
	QPainter p(this);
	style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
}

void BMKeyPad::CargarKeyPad()
{
	m_gridLayoutKeyPadNumero=new QGridLayout();
	int indexColumna;
	int indexFila;
	
	indexColumna=2;
	indexFila=0;
	
	
	//========== TECLAS NUMERICAS ===========
	m_grupoBotones=new QButtonGroup(this);

	for(int i=KEY_BTN_MAX_NUMBER ; i>0 ; i--)
	{
		QString str;
		str.setNum(i);
		ButtonKeyPad* btnKey=new ButtonKeyPad("",QIcon("./resources/BMIcoNum_"+str+".png"),ButtonKeyPad::BUTTON_NUMERO);
		btnKey->m_NumTecla=str;
		
		m_grupoBotones->addButton( btnKey, i );
		m_gridLayoutKeyPadNumero->addWidget(btnKey, indexFila, indexColumna);

		if(indexColumna==0)
		{
			indexColumna=2;
			indexFila++;

		}else
		{
			
			indexColumna--;
		}

	}

//=========== BOTONES DE CEROS Y EL PUNTO =============
	ButtonKeyPad* btnKeyCero=new ButtonKeyPad("",QIcon("./resources/BMIcoNum_0.png"),ButtonKeyPad::BUTTON_HORIZONTAL);
	btnKeyCero->setFixedWidth( btnKeyCero->iconSize().width()*2.3 );
	//btnKeyCero->setFixedHeight(btnKeyCero->iconSize().height());
	btnKeyCero->m_NumTecla="0";
	m_grupoBotones->addButton( btnKeyCero, 0 );

	
	ButtonKeyPad* btnKeyDCero=new ButtonKeyPad("",QIcon("./resources/BMIcoNum_00.png"),ButtonKeyPad::BUTTON_HORIZONTAL);
	btnKeyDCero->setFixedWidth( btnKeyCero->iconSize().width()*2.3 );
	//btnKeyDCero->setFixedHeight(btnKeyCero->iconSize().height());
	btnKeyDCero->m_NumTecla="00";
	m_grupoBotones->addButton( btnKeyDCero, KEY_BTN_DOBLE_CERO );

	
	ButtonKeyPad* btnKeyPunto=new ButtonKeyPad("",QIcon("./resources/BMIcoNum_pto.png"),ButtonKeyPad::BUTTON_NUMERO);
	btnKeyPunto->m_NumTecla=".";
	m_grupoBotones->addButton( btnKeyPunto, KEY_BTN_PUNTO );


	//m_grupoBotones->addButton( btnKeyCero, 0 );
	//m_gridLayoutKeyPadNumero->addWidget(btnKeyCero, 3, 0);

	//m_grupoBotones->addButton( btnKeyDCero, KEY_BTN_DOBLE_CERO );
	//m_gridLayoutKeyPadNumero->addWidget(btnKeyDCero, 3, 1);

	//m_grupoBotones->addButton( btnKeyPunto, KEY_BTN_PUNTO );
	//m_gridLayoutKeyPadNumero->addWidget(btnKeyPunto, 3, 2);

	QHBoxLayout* layoutCeroPunto=new QHBoxLayout();
	layoutCeroPunto->setMargin(0);
	layoutCeroPunto->addWidget(btnKeyCero, 0, Qt::AlignLeft);
	layoutCeroPunto->addWidget(btnKeyDCero, 0, Qt::AlignLeft);
	layoutCeroPunto->addWidget(btnKeyPunto, 0, Qt::AlignLeft);


//================= TECLAS DE ACEPTAR Y BORRAR ==================
	m_btnCancelar=new ButtonKeyPad("",QIcon("./resources/BMIcoNum_Cancel.png"), ButtonKeyPad::BUTTON_NUMERO);
	m_grupoBotones->addButton( m_btnCancelar, KEY_BTN_CANCELAR );

	m_btnAceptar=new ButtonKeyPad("",QIcon("./resources/BMIcoNum_Ok.png"), ButtonKeyPad::BUTTON_VERTICAL);
	//QSize sizes=btnAceptar->iconSize()*1.5;
	//btnAceptar->setFixedHeight(sizes.height()*3+sizes.height()/7);
	QSize sizes=m_btnAceptar->iconSize();
	m_btnAceptar->setFixedHeight(sizes.height()*3+sizes.height()/7);
	m_grupoBotones->addButton( m_btnAceptar, KEY_BTN_ACEPTAR );
//================ Layout que contiene los Botones de Cancel y Ok. ==============
	m_verticalControlsLayout=new QVBoxLayout();
	m_verticalControlsLayout->setMargin(0);
	m_verticalControlsLayout->addWidget(m_btnCancelar);
	m_verticalControlsLayout->addWidget(m_btnAceptar);
	m_verticalControlsLayout->setAlignment(Qt::AlignTop);
	



//================ Layout que contiene la grilla de numeros de 1-9, seguido por el Boton de Ceros y el punto ===================
	m_HorizontalNumControl=new QHBoxLayout();
	m_HorizontalNumControl->addLayout( m_gridLayoutKeyPadNumero );
	m_HorizontalNumControl->addLayout( m_verticalControlsLayout );


//================ Integro al layou de la Clase principal ========================
	m_verticalLayoutKeyPadMain=new QVBoxLayout();
	//m_horizontalLayoutKeyPadMain->addLayout(m_gridLayoutKeyPadNumero);
	m_verticalLayoutKeyPadMain->addLayout( m_HorizontalNumControl );
	m_verticalLayoutKeyPadMain->addLayout( layoutCeroPunto );

	connect( m_grupoBotones, SIGNAL(buttonClicked(int)), this, SLOT(slot_setKeyValuePad(int)) );
	this->setLayout( m_verticalLayoutKeyPadMain );
	
	if(m_textWidget)
		this->move(m_textWidget->x(),m_textWidget->y()+70);
	
	this->show();
}

void BMKeyPad::setWidgetText(QWidget* TextReferences)
{
	m_textWidget=dynamic_cast<QLineEdit*>(TextReferences);
	
	if(m_textWidget)
	{
		m_textWidgetAUX = m_textWidget;
		m_textWidget->setEnabled(true);
		m_textWidget->setFocus();

		disconnect(m_btnAceptar , SIGNAL(clicked()), m_textWidget, SLOT(slot_confirmaFinLinea()) );	
		disconnect( this, SIGNAL(signal_lineaComplete()), m_textWidget, SLOT(slot_confirmaFinLinea()) );
		disconnect( this, SIGNAL(signal_GoBackLineEdit()), m_textWidget, SLOT(slot_goBackLineEdit()) );

		connect(m_btnAceptar , SIGNAL(clicked()), m_textWidget, SLOT(slot_confirmaFinLinea()) );	
		connect( this, SIGNAL(signal_lineaComplete()), m_textWidget, SLOT(slot_confirmaFinLinea()) );
		connect( this, SIGNAL(signal_GoBackLineEdit()), m_textWidget, SLOT(slot_goBackLineEdit()) );
		
	}
	else
	{
		disconnect(m_btnAceptar , SIGNAL(clicked()), m_textWidget, SLOT(slot_confirmaFinLinea()) );	
		disconnect( this, SIGNAL(signal_lineaComplete()), m_textWidget, SLOT(slot_confirmaFinLinea()) );
		disconnect( this, SIGNAL(signal_GoBackLineEdit()), m_textWidget, SLOT(slot_goBackLineEdit()) );
	}
}

void BMKeyPad::setearTamAUX(QRect Botonera, QRect BMAPP)
{
	m_tamBarraBotones=Botonera;
	m_tamBMAPP=BMAPP;
	
}

void BMKeyPad::setNuevaPosicionKeyPad()
{
	if(!m_textWidget)
		return;

	if(m_textWidget->y() > this->y() && m_textWidget->x() < (this->x() + this->width()))
	{
		m_XKEYPAD=-this->width();
		m_YKEYPAD=m_tamBMAPP.y();
		this->move(m_XKEYPAD, m_YKEYPAD);
	}

	else if( m_textWidget->y() > this->y() )
	{
		if( m_textWidget->x()< ( this->x()+this->width() ) )
		{
			m_YKEYPAD= m_tamBMAPP.x() + m_tamBarraBotones.size().width();
		}
	}
	
	else
	{
		m_XKEYPAD=-this->width();
		m_YKEYPAD=m_tamBMAPP.size().height() - this->size().height();
		this->move(m_XKEYPAD, m_YKEYPAD);

	}

}

void BMKeyPad::slot_keyPad_Deslizamiento()
{
	if(m_textWidgetAUX)
		m_textWidget=m_textWidgetAUX;

	if(!m_textWidget)
		return;

	if(!m_animacion)
		m_animacion=new QPropertyAnimation(this, "geometry", this);
	m_animacion->setDuration(1000);
	if(!m_isShow)
	{
		//m_animacion->setStartValue( QRect(-this->width(), this->y(), this->width(), this->height()) );
		//m_animacion->setEndValue( QRect(0, this->y(), this->width(), this->height()) );
		setNuevaPosicionKeyPad();
		m_animacion->setStartValue( QRect(m_XKEYPAD, m_YKEYPAD, this->width(), this->height()) );
		m_animacion->setEndValue( QRect(m_tamBarraBotones.size().width(), m_YKEYPAD, this->width(), this->height()) );
		m_isShow=true;

	}
	else
	{
		//m_animacion->setStartValue( QRect(0, this->y(), this->width(), this->height()) );
		//m_animacion->setEndValue( QRect(-this->width(), this->y(), this->width(), this->height()) );
		m_animacion->setStartValue( QRect(m_tamBarraBotones.size().width(), m_YKEYPAD, this->width(), this->height()) );
		m_animacion->setEndValue( QRect(m_XKEYPAD, m_YKEYPAD, this->width(), this->height()) );
		m_isShow=false;
		
		//Cierro toda conexion de referenc
		slot_TerminalConexion();
	}
	
	m_animacion->start();
}

void BMKeyPad::slot_setKeyValuePad(int Tecla)
{
	if(!m_textWidget)
		return;

	QString aux;
	aux.setNum(Tecla);
	if( Tecla>=0 && Tecla<=KEY_BTN_MAX_NUMBER )
	{
		textInsert(aux);
	}
	else if(Tecla==KEY_BTN_CANCELAR && m_campoTexto.size()==0)
	{
		slot_limpioCampoText();
		
	}
	
	else if(Tecla==KEY_BTN_DOBLE_CERO)
	{
		textInsert("00");
	}
	else if(Tecla==KEY_BTN_PUNTO)
	{
		textInsert(".");
	}

	aux.clear();

	if( m_textWidget->maxLength() == m_textWidget->text().length() )
	{	
		emit signal_lineaComplete();
	}

}

void BMKeyPad::slot_limpioCampoText()
{
	if(m_textWidget->text().size()>0)
		m_textWidget->clear();
	else
		emit signal_GoBackLineEdit();
}

void BMKeyPad::textInsert(QString texto)
{
	if(m_textWidget)
	{

		if(m_textWidget->text().size()==0)
			m_textWidget->setText(texto);
		else
		{
			QString txt;
			txt.append(m_textWidget->text());
			txt.append(texto);
			m_textWidget->setText(txt);
		}

	}

}

void BMKeyPad::slot_TerminalConexion()
{
	if(m_textWidget)
	{
		disconnect(m_btnAceptar , SIGNAL(clicked()), m_textWidget, SLOT(slot_confirmaFinLinea()) );	
		disconnect( this, SIGNAL(signal_lineaComplete()), m_textWidget, SLOT(slot_confirmaFinLinea()) );
		disconnect( this, SIGNAL(signal_GoBackLineEdit()), m_textWidget, SLOT(slot_goBackLineEdit()) );
		if(m_isShow)
			slot_keyPad_Deslizamiento();
		m_textWidget=NULL;
	}
}

//===================================== CLASE ButtonKeyPad ============================================

ButtonKeyPad::ButtonKeyPad(QString text, QIcon ico, char alineacionButton, QWidget *parent):BMButton(text, ico,parent)
{
	m_NumTecla.clear();
	QString styleSheet;
	switch(alineacionButton)
	{
		case BUTTON_HORIZONTAL:
			styleSheet.append("QPushButton { border-image:url(\"./Resources/BMIcoKeyButton_Horizontal.png\"); } ");
			styleSheet.append ("QPushButton:pressed { border-image:url(\"./Resources/BMIcoKeyButton_HorizontalPressed.png\");border-color: rgba(255,255,0,0.4); border: 1px solid #445511; color: #4189C1 };");
			break;

		case BUTTON_VERTICAL:
			styleSheet.append("QPushButton { border-image:url(\"./Resources/BMIcoKeyButton_Vertical.png\"); } ");
			styleSheet.append ("QPushButton:pressed { border-image:url(\"./Resources/BMIcoKeyButton_VerticalPressed.png\");border-color: rgba(255,255,0,0.4); border: 1px solid #445511; color: #4189C1 };");
			break;

		case BUTTON_SHORT:
			styleSheet.append("QPushButton { border-image:url(\"./Resources/BMIcoKeyButton_Short.png\");} ");
			styleSheet.append ("QPushButton:pressed { border-image:url(\"./Resources/BMIcoKeyButton_ShortPressed.png\");border-color: rgba(255,255,0,0.4); border: 1px solid #445511; color: #4189C1 };");
			break;

		case BUTTON_NUMERO:
			styleSheet.append("QPushButton { border-radius: 5px; border-image:url(\"./Resources/BMIcoKeyButton_Num.png\"); } ");
			styleSheet.append ("QPushButton:pressed { border-image:url(\"./Resources/BMIcoKeyButton_NumPressed.png\");border-color: rgba(255,255,0,0.4); border: 1px solid #445511; color: #4189C1 };");
			break;
	}

	this->setStyleSheet(styleSheet);
	this->setIcon(ico);
	this->setFixedSize(this->iconSize()*1.5);
}

ButtonKeyPad::~ButtonKeyPad()
{

}
